package com.service.atf.base.core;

import com.service.atf.base.utils.ATFConstants;
import com.service.atf.base.utils.ATFLogger;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * User: luotao
 * Date: 12-2-27
 * Time: 16:47:00
 */
public class NIOFileReader {
    public static byte[] readFileContent(String path, int len) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(ATFConstants.AC_FILE_PATH_PREFIX + path, "rw");
            FileChannel channel = randomAccessFile.getChannel();
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, len);
            int len_content = buffer.remaining();
            ATFLogger.sysdebug("len_content : " + len_content);
            byte[] result = new byte[buffer.remaining()];
            ATFLogger.sysdebug("result = " + result[result.length - 1]);
            buffer.get(result);
            randomAccessFile.close();
            channel.close();
            return result;
        } catch (Exception e) {
            String msg = "恢复AC，读取快照文件时发生错误 : " + e.toString();
            ATFLogger.syserr(msg);
            throw new RuntimeException(msg);
        }
    }
}
